package test.android.http;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

/**
 *
 * @author dpogibenko
 */
public class DataReceiver {
    private HttpURLConnection urlConnection;
    private Element xmlRoot;
    private Node currentStore;
    
    public DataReceiver(){
        
    } 
    
    public boolean httpConnect(String urlAddress) {      
        try {
            URL url = new URL(urlAddress);
            urlConnection = (HttpURLConnection) url.openConnection();           
            return true;
        } catch (Exception ex) {
            return false;
        }            
    }
       
    public void closeConnection() {
        urlConnection.disconnect();
        urlConnection = null;
        xmlRoot = null;
        currentStore = null;
    }
    
    public void parseXML(InputStream in) {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(in);
            xmlRoot = doc.getDocumentElement();
            //nextStore(root);

        } catch (SAXException ex) {
            Logger.getLogger(HTTPActivity.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(HTTPActivity.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(HTTPActivity.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    
    private void initXML() {
        InputStream in = null;
        try {
            in = urlConnection.getInputStream();
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(in);
            xmlRoot = doc.getDocumentElement();
        } catch (SAXException ex) {
            Logger.getLogger(DataReceiver.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(DataReceiver.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DataReceiver.class.getName()).log(Level.SEVERE, null, ex);
        } finally {

        }
    }
    
    public String nextRow() {
        if (xmlRoot==null){
            this.initXML();
        }
        if (this.nextStore()==true){
            return(this.getSales());            
        }        
        return null;
    }

    private Boolean nextStore() {
        if (currentStore==null){
            currentStore = xmlRoot.getFirstChild();
        }
        else {
            currentStore = currentStore.getNextSibling();
        }
        while ((currentStore = currentStore.getNextSibling())!=null) {
            if (currentStore instanceof Element) {
                return true;
            }
        }
        return false;
        
    }

    private String getSales() {
        //String number = store.getAttribute("number");
        Element store = (Element)currentStore;
        String number = store.getAttribute("number");
        StringBuilder sales = null;
        StringBuilder returns = null;
        Node child = store.getFirstChild();
        while (child != null) {
            if (child instanceof Element) {
                Element childElement = (Element) child;
                String name = childElement.getTagName();
                String data = childElement.getTextContent();
                if (name.equalsIgnoreCase("sales")) {
                    sales = new StringBuilder(data);
                } else if (name.equalsIgnoreCase("returns")) {
                    returns = new StringBuilder(data);
                }
            }
            child = child.getNextSibling();
        }
        if (sales == null) {
            sales = new StringBuilder("0");
        }
        if (returns == null) {
            returns = new StringBuilder("0");
        }
        return (number + "\t" + sales + "\t" + returns);
    }
}
